import 'dart:async';

import 'package:flutter/cupertino.dart';

/* 防止重复动作工具方法 */
class PreventRepeatActionUtil {
  static const Duration _defaultDelay = Duration(milliseconds: 300);

  @protected
  static bool _enable = true;

  static void Function()? throttle(Function action, {Duration delay = _defaultDelay}) {
    return () {
      if (_enable) {
        action();
        _enable = false;
        Future.delayed(delay, () {
          _enable = true;
        });
      }
    };
  }

  @protected
  static Timer? _timer;
  static void Function()? debounce(Function action, {Duration delay = _defaultDelay}) {
    return () {
      _timer?.cancel();
      _timer = Timer(delay, () {
        action();
        _timer = null;
      });
    };
  }
}
